//
//  WMEFirstPageInformationVC.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/1/20.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEFirstPageInformationVC.h"
#import "WMEInformationTableViewCell.h"
#import "WMEUserViewController.h"
#import "WMEMoreViewController.h"
#import <Masonry.h>
#import <AFNetworking.h>
#import "DataBaseManager.h"
#import <UMSocial.h>
#pragma mark - 首页详情页的VC
@interface WMEFirstPageInformationVC ()<UITableViewDataSource, UITableViewDelegate, tiaozhuangDelegate,moreDelegate>
@property (nonatomic, retain)NSMutableDictionary *dataDic;
@property (nonatomic, retain)UITableView *tableView;
@end

@implementation WMEFirstPageInformationVC

- (void)dealloc
{
    [_dataDic release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //关闭navigation的半透明
    self.navigationController.navigationBar.translucent = NO;
    //创建右上按钮
    UIBarButtonItem *collect = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(collectAction:)];
    UIBarButtonItem *fenxiang = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(fenxiangAction:)];
    self.navigationItem.rightBarButtonItems = @[collect, fenxiang];
    self.navigationItem.title = @"详情";
    [collect release];
    [fenxiang release];
    
    //创建视图
    [self createView];
    //数据请求
    [self data];
}
//数据请求
- (void)data
{
    NSString *temp = @"http://www.duitang.com/napi/blog/detail/?__dtac=%257B%2522_r%2522%253A%2520%2522203659%2522%257D&app_code=gandalf&app_version=5.8%20rv%3A149591&blog_id=512642603&device_name=iPhone%205&device_platform=iPhone5%2C2&include_fields=tags%2Crelated_albums%2Crelated_albums.covers%2Croot_album%2Cshare_links_2%2Cextra_html%2Ctop_comments%2Ctop_like_users&locale=zh_CN&platform_name=iPhone%20OS&platform_version=9.2&screen_height=568&screen_width=320&top_comments_count=12&top_forward_users_count=8&top_like_users_count=8";
    NSString *str = [temp stringByReplacingOccurrencesOfString:@"512642603" withString:_jiekou];
    NSLog(@"~~!!~~!!%@", _jiekou);
    AFHTTPSessionManager *data = [AFHTTPSessionManager manager];
    [data GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@", responseObject);
        self.dataDic = responseObject[@"data"];
//        NSLog(@"%@", _dataDic);
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        nil;
    }];
}
//创建视图
- (void)createView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    __block __typeof(&*self)ws = self;
    [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.view.mas_left).with.offset(0);
        make.right.equalTo(ws.view.mas_right).with.offset(0);
        make.top.equalTo(ws.view.mas_top).with.offset(0);
        make.bottom.equalTo(ws.view.mas_bottom).with.offset(0);
    
    }];
    _tableView.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.bounces = NO;
//    _tableView.separatorStyle = NO;
    _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [_tableView release];
    
}
//个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //整个界面就一个cell
    return 1;
}
//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_tableView registerClass:[WMEInformationTableViewCell class] forCellReuseIdentifier:@"cell"];
    WMEInformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //数据保护
    if (!_dataDic) {
        return cell;
    }
    cell.dic = _dataDic;
    cell.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1];
    //cell不能被点击
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //签协议
    cell.delegate = self;
    //签协议
    cell.moDelagete = self;
    return cell;
}
//设置 cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
#warning 协议方法 如果数据为空时 返回随便一个值 不然会蹦
    if (_dataDic) {
//        见名知意
        CGFloat imageHeight = [WMEInformationTableViewCell heightWithImage:_dataDic[@"photo"][@"width"] height:_dataDic[@"photo"][@"height"]];
        CGFloat textHeight = [WMEInformationTableViewCell heightWithText:_dataDic[@"msg"]];
        NSInteger CommentCount = [_dataDic[@"reply_count"] integerValue];
        if ([_dataDic[@"like_count"] integerValue] != 0) {
            if (CommentCount > 0) {
                if (CommentCount > 4) {
                    CommentCount = 4;
                }
                //评论的高
                CGFloat plHeight = 70 * CommentCount;
                return  imageHeight + textHeight + 80 + 10 + 200 + plHeight;
            }else {
                return  imageHeight + textHeight + 150 + 80;
            }
        }else{
            if (CommentCount > 0) {
                if (CommentCount > 4) {
                    CommentCount = 4;
                }
                CGFloat plHeight = 70 * CommentCount;
                return imageHeight + textHeight + plHeight + 100;
            }else {
                return imageHeight + textHeight + 100;
            }
        }
    }
    return 0;
    
}
#pragma mark - 协议方法
- (void)tiaotiaotiao:(NSString *)string userDic:(NSDictionary *)dictionary
{
    WMEUserViewController *userVC = [[WMEUserViewController alloc] init];
    UINavigationController *userNavi = [[UINavigationController alloc] initWithRootViewController:userVC];
    userVC.userId = string;
    userVC.userDic = dictionary;
    [self presentViewController:userNavi animated:YES completion:^{
    }];
    [userVC release];
}
- (void)tiaodDaoMore:(NSString *)str maxCount:(NSNumber *)max
{
    WMEMoreViewController *more = [[WMEMoreViewController alloc] init];
    //    UINavigationController *moreNavi = [[UINavigationController alloc] initWithRootViewController:more];
    more.str = str;
    more.maxCount = max;
    [self.navigationController pushViewController:more  animated:YES];
    [more release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)collectAction:(UIBarButtonItem *)btn
{
    // 创建单例对象
    DataBaseManager *dbManger = [DataBaseManager shareInstance];
    [dbManger openDB];
    // 创建表
    [[DataBaseManager shareInstance] createTable];
    
    // 查询表
    NSMutableArray *arr1 = [[DataBaseManager shareInstance] select];
    if (arr1) {
        
        for (Movie *model in arr1) {
            if ([model.pubdate isEqualToString:_jiekou]) {
                //创建
                UIAlertController *al = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经收藏" preferredStyle:UIAlertControllerStyleAlert];
                //显示
                [self presentViewController:al animated:YES completion:^{
                    // 一秒后执行diss方法
                    [self performSelector:@selector(diss:) withObject:self afterDelay:1];
                    
                }];
                NSLog(@"已经收藏 ");
                return;
            }
        }
        
    }
    // 创建数据库类
    Movie *m = [[Movie alloc] init];
    // 把需要放入数据库的数据防区数据库
    // 主文本
    m.movieName = _dataDic[@"msg"];
    // Id
    m.pubdate = _jiekou;
    // 调用数据库方法
    [[DataBaseManager shareInstance] insertMovie:m];
    //创建
    UIAlertController *al = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
    //显示
    [self presentViewController:al animated:YES completion:^{
        // 一秒后执行diss 方法
        [self performSelector:@selector(diss:) withObject:self afterDelay:1];
        
    }];
    
}

- (void)diss:(id)a
{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
    
}
- (void)fenxiangAction:(UIBarButtonItem *)btn
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"56a8767e67e58eaba7000c68" shareText:@"分享测试"
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ, UMShareToTencent,nil]
                                       delegate:self];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
